<?php if (!defined('NOVA_RUN')) die('You can\'t access this file directly!');

/*
-------------------------------------------------------
*
*   NovaBoard 2
*   Copyright © 2011 NovaBoard team
*
*--------------------------------------------------------
*
*   Official site: www.novaboard.net
*   Contact e-mail: support@novaboard.net
*
*   GNU General Public License, version 2:
*   http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
*
---------------------------------------------------------
*/

class Category_Action extends Admin_Action
{
	// current action name
	var $action = 'index';

	// avaliable methods
	var $methods = array('view');

	// default method
	var $default = 'view';

	public function view()
	{
		// get categories
		$catModel = new Category_Model();
		$CurId = (!Router::param(0)) ? 0 : Router::param(0);

		if ((!is_numeric($CurId)) || ($CurId != 0 && !$catModel->checkCategoryExist($CurId))) {
			Engine::ShowError(6);
		}

		$curCat = $catModel->getCategoryInfo($CurId);
		$cats = $catModel->getCategories($CurId);

		$error = (sizeof($cats) < 1) ? true : false;

		tpl::instance()->assign('curCat', $curCat);
		tpl::instance()->assign('error', $error);
		tpl::instance()->assign('categories', $cats);
		tpl::instance()->show('category/view');
	}

	public function add()
	{
		$error = false;

		if (get_request('add_category', 'post') == 1)
		{
			Engine::secretKey(true);
			if (!get_request('name', 'post') || !get_request('description', 'post') || !get_request('order', 'post'))
			{
				$error = 'Please check that you filled in all the fields';
			}
			elseif (!is_numeric(get_request('order', 'post')))
			{
				$error = 'Field order should be a numeric';
			}
			else
			{
				$catModel = new Category_Model();
				if (
					(is_numeric(get_request('parent', 'post'))
					&& get_request('parent', 'post') > 0
					&& !$catModel->checkCategoryExist(get_request('parent', 'post')))
				)	Engine::showError(6);

				$id = $catModel->addCategory((is_numeric(get_request('parent', 'post')) && get_request('parent', 'post') > 0) ? get_request('parent', 'post') : 0);

				// redirect to new category
				redirect(make_admin_link('category', 'view', array($id)));
			}
		}

		tpl::instance()->assign('error', $error);
		tpl::instance()->show('category/add');
	}

	public function permissions()
	{
		// TODO: don't forget about 'soft' removing
		$catModel = new Category_Model();
		if (get_request('set_permissions', 'post') == 1)
		{

		}

		if (Router::param(0) > 0 && is_numeric(Router::param(0)) && $catModel->checkCategoryExist(Router::param(0)))
		{
				if (Router::param(1) != null && is_numeric(Router::param(1)) && $this->UserModel->checkGroupExist(Router::param(1)))
				{
					$success = null;
					if (get_request('set_permissions', 'post') == 1)
					{
						// category id and group id are taken from get, 'cuz they are already checked above
						Engine::secretKey(true);
						$catModel->updatePermissions(Router::param(0), Router::param(1));
						$success = 'Permissions have been updated';
					}

					tpl::instance()->assign('permissions', $catModel->getCategoryPermissions(Router::param(0), Router::param(1)));
					tpl::instance()->assign('success', $success);
					tpl::instance()->show('category/permissions_set');
				}
				else
				{
					tpl::instance()->assign('groups', $this->UserModel->getGroups());
					tpl::instance()->show('category/permissions_view');
				}
		}
		else
		{
			Engine::showError(6);
		}
	}
}